﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Web;
using System.Text;
using System.Web.UI.WebControls;
using MLY;

namespace MLY.Bll
{
    public class ExcelExport
    {
        public static void PrepareCsvFileFromList(string[][] lst, string FileName)
        {
            string separator = ",";
            string eol = "\n";
            string filetext = "";

            for (int i = 0; i<lst.Length; i++)
            {
                for (int j = 0; j < lst[i].Length; j++)
                {
                    filetext += lst[i][j].Trim().Replace(",", ".").Replace("\r\n", "");
                    filetext += separator;
                }
                filetext += eol;
            }

            sendFileToClient(filetext, FileName);
        }
        
        //public static string PrepareCsvFile(List<string[]> Rows, 
        public static void PrepareCsvFileFromGridview(GridView grid, string FileName)
        {
            string separator = ",";
            string eol = "\n";
            string filetext = "";
            string celltext;

            foreach (TableCell cell in grid.HeaderRow.Cells)
            {
                filetext += cell.Text.Replace("&nbsp;", "") + separator;
            }
            filetext += eol;

            foreach (GridViewRow row in grid.Rows)
            {
                foreach (TableCell cell in row.Cells)
                {
                    celltext = cell.Text.Replace("&nbsp;", "").Trim().Replace(",", ".");

                    foreach (System.Web.UI.Control c in cell.Controls)
                    {
                        //if (c is System.Web.UI.Control)
                        //{
                            try
                            {
                                celltext += (c as System.Web.UI.DataBoundLiteralControl).Text.Replace("\r\n", "").Trim().Replace(",", ".");
                            }
                            catch { }
                        //}
                    }
                    filetext +=  celltext + separator;

                }
                filetext += eol;
            }


            sendFileToClient(filetext, FileName);
        }

        public static void sendFileToClient(string filetext, string FileName)
        { 
            string FileNameExtention = DateTime.Now.Year.ToString() + 
                                       DateTime.Now.Month.ToString() + 
                                       DateTime.Now.Day.ToString() + 
                                       DateTime.Now.Hour.ToString() + 
                                       DateTime.Now.Minute.ToString() + 
                                       DateTime.Now.Second.ToString() + 
                                       DateTime.Now.Millisecond.ToString();
            string FinalFileName = FileName + FileNameExtention + ".csv";

            // saving text into a file on the server
            StreamWriter sr = new StreamWriter(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "Files\\" + FinalFileName, true, Encoding.GetEncoding("windows-1255"));
            sr.Write(filetext);
            sr.Flush();
            sr.Close();

            // try 3:
            FileInfo OutFile = new FileInfo(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "Files\\" + FinalFileName);
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + FinalFileName);
            System.Web.HttpContext.Current.Response.ContentType = "application/vnd.xls";
            System.Web.HttpContext.Current.Response.TransmitFile(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "Files\\" + FinalFileName, 0, OutFile.Length);
            System.Web.HttpContext.Current.Response.Flush();
            System.Web.HttpContext.Current.Response.End();

            // try 2:
            //System.Web.HttpContext.Current.Response.Redirect("~/Files/" + FinalFileName, false);
            
            // try 1:
            //System.Web.HttpContext.Current.Response.Buffer = true;
            //System.Web.HttpContext.Current.Response.Clear();
            //System.Web.HttpContext.Current.Response.ClearContent();
            //System.Web.HttpContext.Current.Response.ClearHeaders();
            //System.Web.HttpContext.Current.Response.ContentType = "application/vnd.xls";
            
            //System.Web.HttpContext.Current.Response.WriteFile(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "files\\" + FinalFileName);
            
            // sendign the file to a client
            //System.Web.HttpContext.Current.Response.Buffer = true;
            //System.Web.HttpContext.Current.Response.Clear();
            //System.Web.HttpContext.Current.Response.ClearContent();
            //System.Web.HttpContext.Current.Response.ClearHeaders();
            //System.Web.HttpContext.Current.Response.ContentType = "application/vnd.xls";

            ////System.Web.HttpContext.Current.Response.OutputStream.Write(filetext, 0, filetext.Length);
            //System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + FinalFileName);
            //System.Web.HttpContext.Current.Response.Charset = "";
            //System.Web.HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

            //System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            //System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
            ////htmlWrite.Write(filetext);            
            ////filetext.RenderControl(htmlWrite);

            //System.Web.HttpContext.Current.Response.Write(stringWrite.ToString());
            //System.Web.HttpContext.Current.Response.End();
            //System.Web.HttpContext.Current.Response.Flush();
            //System.Web.HttpContext.Current.Response.Clear();        
       
             
        }
        
    }
}
